﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using whatpricemyhouse.LINQ;

namespace whatpricemyhouse.Buy
{
    public partial class BuyForm : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ImageButton1.Attributes.Add("onmouseover", "mouseOver1(this);");
            ImageButton1.Attributes.Add("onmouseout", "mouseOut1(this);");

            if (Session["areaName"] == null)
            {
                Response.Redirect("~/default.aspx?e=T");
            }
            else if (Session["areaName"].ToString() == "")
            {
                Response.Redirect("~/default.aspx?e=T");
            }
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            //check form
            //check User information
            bool redirectOK = false;

            //--Custoemr ID
            String CustomerID = Guid.NewGuid().ToString();
            //-----save customer

            DataControllerDataContext dcdc = new DataControllerDataContext();


            customer_table ct = new customer_table();
            ct.CID = CustomerID;
            ct.CName = this.UserNameTxt.Text;
            ct.CEmail = this.UserEmailTxt.Text;
            ct.CPhone = this.UserPhoneTxt.Text;
            ct.CCreateDate = DateTime.Now;
            dcdc.customer_tables.InsertOnSubmit(ct);



            //--Buy Request ID
            String BuyRequestID = Guid.NewGuid().ToString();
            //save data to database
            //--save buy request
            buy_request_table brt = new buy_request_table();
            brt.BRID = BuyRequestID;
            brt.CID = CustomerID;
            brt.is_surrounding = this.interestSurroundCheckBox.Checked;
            brt.suburbid = this.placeSelector.getSelectedValue();
            brt.price_range_from = this.priceRangeSelectorFrom.getSelectedValue();
            brt.price_range_to = this.priceRangeSelectorTo.getSelectedValue();
            brt.PTID = this.propertyTypeSelector.getSelectedValue();
            brt.WTBID = this.whenToBuySelector.getSelectedValue();
            brt.createdDate = DateTime.Now;
            brt.isOld = true;
            brt.visa = 0;
            dcdc.buy_request_tables.InsertOnSubmit(brt);
            //--save buy_loan table



            List<String> loanCheckList = loanTypeChecker.getCheckList();
            foreach (String v in loanCheckList)
            {
                buy_request__loan_type brlt = new buy_request__loan_type();
                brlt.BRID = BuyRequestID;
                brlt.LTID = v;
                dcdc.buy_request__loan_types.InsertOnSubmit(brlt);
            }

            dcdc.SubmitChanges();

            //go to thank you page
            Response.Redirect("~/PublicPage/ThankYou.aspx");
        }

    }
}